package com.hpin.fansea.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.CommentList;
import com.hpin.fansea.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/9/9.
 */
public class CommentAdapter extends BaseAdapter {

    private Context context;
    private List<CommentList> purchaseYlCommentLists;
    public DisplayImageOptions options;

    public CommentAdapter(Context context, List<CommentList> purchaseYlCommentLists) {
        this.context = context;
        this.purchaseYlCommentLists = purchaseYlCommentLists;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_headpic_default)
                .showImageForEmptyUri(R.mipmap.icon_headpic_default)
                .showImageOnFail(R.mipmap.icon_headpic_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    public void setData(List<CommentList> purchaseYlCommentLists) {
        this.purchaseYlCommentLists = purchaseYlCommentLists;
    }

    @Override
    public int getCount() {
        return purchaseYlCommentLists.size();
    }

    @Override
    public Object getItem(int position) {
        return purchaseYlCommentLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_comment, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_img = (CircleImageView) convertView.findViewById(R.id.item_commemt_avatar);
            viewHolder.tv_tittle = (TextView) convertView.findViewById(R.id.item_commemt_pthone);
            viewHolder.tv_des = (TextView) convertView.findViewById(R.id.item_commemt_content);
            viewHolder.item_commemt_time = (TextView) convertView.findViewById(R.id.item_commemt_time);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 评论人nickname
        viewHolder.tv_tittle.setText(purchaseYlCommentLists.get(position).nickname);

        // 评论人头像
        ImageLoader.getInstance().displayImage(purchaseYlCommentLists.get(position).headPic, viewHolder.tv_img, options);

        // 评论内容
        viewHolder.tv_des.setText(purchaseYlCommentLists.get(position).commentCont);

        // 评论时间
        viewHolder.item_commemt_time.setText(purchaseYlCommentLists.get(position).commentTime);

        return convertView;
    }

    private class ViewHolder {
        public CircleImageView tv_img;
        public TextView tv_tittle;
        public TextView tv_des;
        public TextView item_commemt_time;
    }
}
